﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Think.Lenovo.Com.DataAccess;
using MySql.Data.MySqlClient;
using System.Data;
using System.Reflection;
using Think.Lenovo.Com.Models.Material;

namespace Think.Lenovo.Com
{
    public partial class product : System.Web.UI.Page
    {
        //产品分类导航页面
        productClassInfo classInfo = new productClassInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDate();
            }
        }

        #region 绑定左边菜单栏
        public void BindDate()
        {
            DataSet ds = classInfo.GetSubClassList("0");
            Repeater1.DataSource = DataSetToIList<MaterialModel>(ds, "tbl_product_class");
            Repeater1.DataBind();
        }

        /// <summary>
        /// 绑定到左边菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
            {
                MaterialModel nm = (MaterialModel)e.Item.DataItem;

                HyperLink lButton = (HyperLink)e.Item.FindControl("HyperLink1");
                lButton.Text = nm.ClassName.ToString();
                lButton.NavigateUrl = "#";
                lButton.Attributes.Add("onclick","getChildClass('"+nm.ClassId+"')");
            }
        }
        #endregion

        #region 
        #endregion

        #region dataset to ilist
        public IList<T> DataSetToIList<T>(DataSet p_DataSet, string p_TableName)
        {

            int _TableIndex = 0;

            if (p_DataSet == null || p_DataSet.Tables.Count < 0)

                return null;

            if (string.IsNullOrEmpty(p_TableName))

                return null;

            for (int i = 0; i < p_DataSet.Tables.Count; i++)
            {

                // 获取Table名称在Tables集合中的索引值   

                if (p_DataSet.Tables[i].TableName.Equals(p_TableName))
                {

                    _TableIndex = i;

                    break;

                }

            }
            return DataSetToIList<T>(p_DataSet, _TableIndex);

        }

        public IList<T> DataSetToIList<T>(DataSet p_DataSet, int p_TableIndex)
        {

            if (p_DataSet == null || p_DataSet.Tables.Count < 0)

                return null;

            if (p_TableIndex > p_DataSet.Tables.Count - 1)

                return null;

            if (p_TableIndex < 0)
                p_TableIndex = 0;


            DataTable p_Data = p_DataSet.Tables[p_TableIndex];

            // 返回值初始化   

            IList<T> result = new List<T>();
            for (int j = 0; j < p_Data.Rows.Count; j++)
            {

                T _t = (T)Activator.CreateInstance(typeof(T));
                PropertyInfo[] propertys = _t.GetType().GetProperties();

                foreach (PropertyInfo pi in propertys)
                {
                    for (int i = 0; i < p_Data.Columns.Count; i++)
                    {

                        // 属性与字段名称一致的进行赋值   
                        if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
                        {
                            // 数据库NULL值单独处理   
                            if (p_Data.Rows[j][i] != DBNull.Value)

                                pi.SetValue(_t, p_Data.Rows[j][i], null);
                            else

                                pi.SetValue(_t, null, null);
                            break;

                        }

                    }

                }

                result.Add(_t);

            }

            return result;
        }

        #endregion
    }
}